package com.jingdianjichi.auth.infra.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncryptUtil {


    private static String publicKey;
    private static String privateKey;

    //静态代码块的作用一般是类的初始化比如在数据库连接工具类中，可以使用静态代码块来加载数据库驱动，初始化连接池等。
    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            //将密码对分别赋值
            //注意顺序
            //1是publicKey
            //0是privateKey
            publicKey = keyPair[1];
            System.out.println("publicKey = " + publicKey);

            privateKey = keyPair[0];

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }


    public static String encrypet(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt = " + encrypt);
        return encrypt;
    }


    public static void main(String[] args) throws Exception {
        String encrypt = encrypet("admin");
        System.out.println("encrypt = " + encrypt);


    }


}